/*
============================================================================
 Name        : nmsHuffman.h
 Author      : Jian
 Version     :
 Copyright   : base on http://ce.sharif.edu/~ahmadinejad/jpeg/
 Description : nmsHuffman.h - header file
============================================================================
*/

#ifndef NMSHUFFMAN_H
#define NMSHUFFMAN_H

#include "nmsJpegDefine.h"

/* Forward declaration */
struct __nms_jpeg_mcu ;


/*
*  nmsHuffman Use for decode jpeg file
*/
typedef struct __nms_huffman {
    UINT16 firstCodeVal[17] ; /* 0 is not use */
    UINT8  symbolIndex[17] ;
    UINT8  bitWidth[17] ;
    UINT16 checkCount[17] ;
    UINT32 symbolCount ;
    UINT8* symbol ;
}nmsHuffman ;

int nmsJpegHuffmanReadFrom(HFILE fileHandle, struct __nms_huffman* acTable[], struct __nms_huffman* dcTable[]) ;

void nmsJpegHuffmanFree(nmsHuffman* hufm) ;

/* 
* result :
* -1:  error 
* 0 :  succeed
* 1:   succeed and got eob ((0,0) zero count is 0 and value bit len = 0)
*/

#define __EOB_FLAG 1    

int nmsJpegHuffmanParse(const nmsHuffman* hufm, struct __nms_jpeg_mcu* mcu, int isAc, int* val, UINT8* zeroCount) ;


/*
*  nmsHuffmanBuilder Use for encode jpeg file
*/

typedef struct __nms_huffman_val {
    UINT16  code ;
    UINT8   bitLen ;
    #ifdef __NMS_JPEG_DEBUG
    UINT8   posInHt ;
    #endif
}nmsHufmmanVal;

#ifdef __NMS_JPEG_DEBUG

typedef struct __nms_huffman_builder {
    nmsHufmmanVal*  valArray ;
    int             valCount ;
    const UINT8*    htVals ;
}nmsHuffmanBuilder;

nmsHufmmanVal nmsHuffmanGet(nmsHuffmanBuilder* hum, UINT8 val) ;

void nmsHuffmanBuilderFree(nmsHuffmanBuilder* hum) ;

#else

typedef nmsHufmmanVal nmsHuffmanBuilder ;

#define nmsHuffmanGet(hum, val) ((hum)[val])

#define nmsHuffmanBuilderFree(hum) do { if (hum) nmsFreeL(hum); } while (0)

#endif

nmsHuffmanBuilder* nmsHuffmanBuild(const UINT8* htCount,const UINT8* htVals, UINT8 maxVal) ;


#endif  /* NMSHUFFMAN_H */

/* End of File */
